//
//  CModule.m
//  mocha
//
//  Created by Justin Yip on 9/24/13.
//
//

#import "CApplication.h"
#import <Cordova/CDVPlugin.h>


#pragma mark - Application

@interface CApplication (){
    NSMutableArray* moduleArray;
}


@end

@implementation CApplication


#pragma mark Initialize

+(CApplication *)sharedApplication{
    static CApplication *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[CApplication alloc] init];
        // Do any other initialisation stuff here
    });
    return sharedInstance;
}


-(id)init{
    self = [super init];
    if (self){
    }
    return self;
}

#pragma mark metod

-(void)initModulesConfig:(UIApplication *)application didFinishLaunchingWithOptions:(nullable NSDictionary *)launchOptions{
    
    
    NSDictionary* configDict=[NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"config" ofType:@"json"]] options:kNilOptions error:nil];
    
    NSArray* configArray = [configDict objectForKey:@"applicationEntry"];
    
    if(moduleArray==nil)
        moduleArray=[NSMutableArray arrayWithCapacity:2];
    [moduleArray removeAllObjects];
    
    for(NSString* dict in configArray){
        id<UIApplicationDelegate> obj=(id<UIApplicationDelegate>)([[NSClassFromString(dict) alloc] init]);
        if(obj!=nil){
            [moduleArray addObject:obj];
            if([obj respondsToSelector:@selector(application:didFinishLaunchingWithOptions:)])
                [obj application:application didFinishLaunchingWithOptions:launchOptions];
        }
    }
}

#pragma mark - lifecycle

- (void)applicationDidBecomeActive:(UIApplication *)application{
    for(id<UIApplicationDelegate> obj in moduleArray){
        if([obj respondsToSelector:@selector(applicationDidBecomeActive:)])
        [obj applicationDidBecomeActive:application];
    }

}

- (void)applicationWillResignActive:(UIApplication *)application{
    for(id<UIApplicationDelegate> obj in moduleArray){
        if([obj respondsToSelector:@selector(applicationWillResignActive:)])
        [obj applicationWillResignActive:application];
    }
}

- (void)applicationDidEnterBackground:(UIApplication *)application{
    for(id<UIApplicationDelegate> obj in moduleArray){
        if([obj respondsToSelector:@selector(applicationDidEnterBackground:)])
        [obj applicationDidEnterBackground:application];
    }
}

- (void)applicationWillEnterForeground:(UIApplication *)application{
    for(id<UIApplicationDelegate> obj in moduleArray){
        if([obj respondsToSelector:@selector(applicationWillEnterForeground:)])
        [obj applicationWillEnterForeground:application];
    }
}


-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)_deviceToken{
    for(id<UIApplicationDelegate> obj in moduleArray){
        if([obj respondsToSelector:@selector(application:didRegisterForRemoteNotificationsWithDeviceToken:)])
        [obj application:application didRegisterForRemoteNotificationsWithDeviceToken:_deviceToken];
    }
    
}

-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
    for(id<UIApplicationDelegate> obj in moduleArray){
        if([obj respondsToSelector:@selector(application:didFailToRegisterForRemoteNotificationsWithError:)])
        [obj application:application didFailToRegisterForRemoteNotificationsWithError:error];
    }
}

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
    for(id<UIApplicationDelegate> obj in moduleArray){
        if([obj respondsToSelector:@selector(application:didReceiveRemoteNotification:)])
        [obj application:application didReceiveRemoteNotification:userInfo];
    }
}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
    for(id<UIApplicationDelegate> obj in moduleArray){
        if([obj respondsToSelector:@selector(application:didReceiveLocalNotification:)])
        [obj application:application didReceiveLocalNotification:notification];
    }
}




- (void)applicationDidFinishLaunching:(UIApplication *)application{
    for(id<UIApplicationDelegate> obj in moduleArray){
        if([obj respondsToSelector:@selector(applicationDidFinishLaunching:)])
        [obj applicationDidFinishLaunching:application];
    }

}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
    for(id<UIApplicationDelegate> obj in moduleArray){
        if([obj respondsToSelector:@selector(application:handleOpenURL:)])
        [obj application:application handleOpenURL:url];
    }
    return YES;
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation{
    for(id<UIApplicationDelegate> obj in moduleArray){
        if([obj respondsToSelector:@selector(application:openURL:sourceApplication:annotation:)])
        [obj application:application openURL:url sourceApplication:sourceApplication annotation:annotation];
    }
    return YES;

}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options{
    for(id<UIApplicationDelegate> obj in moduleArray){
        if([obj respondsToSelector:@selector(application:openURL:options:)])
        [obj application:application openURL:url options:options];
    }
    return YES;
}

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application{
    for(id<UIApplicationDelegate> obj in moduleArray){
        if([obj respondsToSelector:@selector(applicationDidReceiveMemoryWarning:)])
        [obj applicationDidReceiveMemoryWarning:application];
    }
}

- (void)applicationWillTerminate:(UIApplication *)application{
    for(id<UIApplicationDelegate> obj in moduleArray){
        if([obj respondsToSelector:@selector(applicationWillTerminate:)])
        [obj applicationWillTerminate:application];
    }
}
- (void)applicationSignificantTimeChange:(UIApplication *)application{
    for(id<UIApplicationDelegate> obj in moduleArray){
        if([obj respondsToSelector:@selector(applicationSignificantTimeChange:)])
        [obj applicationSignificantTimeChange:application];
    }
}

- (void)application:(UIApplication *)application willChangeStatusBarOrientation:(UIInterfaceOrientation)newStatusBarOrientation duration:(NSTimeInterval)duration{
    for(id<UIApplicationDelegate> obj in moduleArray){
        if([obj respondsToSelector:@selector(application:willChangeStatusBarOrientation:duration:)])
        [obj application:application willChangeStatusBarOrientation:newStatusBarOrientation duration:duration];
    }
}

- (void)application:(UIApplication *)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation{
    for(id<UIApplicationDelegate> obj in moduleArray){
        if([obj respondsToSelector:@selector(application:didChangeStatusBarOrientation:)])
        [obj application:application didChangeStatusBarOrientation:oldStatusBarOrientation ];
    }
}

- (void)application:(UIApplication *)application willChangeStatusBarFrame:(CGRect)newStatusBarFrame{
    for(id<UIApplicationDelegate> obj in moduleArray){
        if([obj respondsToSelector:@selector(application:willChangeStatusBarFrame:)])
        [obj application:application willChangeStatusBarFrame:newStatusBarFrame ];
    }
}

- (void)application:(UIApplication *)application didChangeStatusBarFrame:(CGRect)oldStatusBarFrame{
    for(id<UIApplicationDelegate> obj in moduleArray){
        if([obj respondsToSelector:@selector(application:didChangeStatusBarFrame:)])
        [obj application:application didChangeStatusBarFrame:oldStatusBarFrame ];
    }
}

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{
    for(id<UIApplicationDelegate> obj in moduleArray){
        if([obj respondsToSelector:@selector(application:didRegisterUserNotificationSettings:)])
        [obj application:application didRegisterUserNotificationSettings:notificationSettings ];
    }
}


- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler{
    for(id<UIApplicationDelegate> obj in moduleArray){
        if([obj respondsToSelector:@selector(application:handleActionWithIdentifier:forLocalNotification:completionHandler:)])
        [obj application:application handleActionWithIdentifier:identifier forLocalNotification:notification  completionHandler:completionHandler];
    }
}

- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void(^)())completionHandler{
    
    for(id<UIApplicationDelegate> obj in moduleArray){
        if([obj respondsToSelector:@selector(application:handleActionWithIdentifier:forRemoteNotification:withResponseInfo:completionHandler:)])
        [obj application:application handleActionWithIdentifier:identifier forRemoteNotification:userInfo  withResponseInfo:userInfo completionHandler:completionHandler];
    }
    
}

- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler{
    for(id<UIApplicationDelegate> obj in moduleArray){
        if([obj respondsToSelector:@selector(application:handleActionWithIdentifier:forRemoteNotification:completionHandler:)])
        [obj application:application handleActionWithIdentifier:identifier forRemoteNotification:userInfo completionHandler:completionHandler];
    }
}

- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void(^)())completionHandler{
    
    
    for(id<UIApplicationDelegate> obj in moduleArray){
        if([obj respondsToSelector:@selector(application:handleActionWithIdentifier:forLocalNotification:withResponseInfo:completionHandler:)])
        [obj application:application handleActionWithIdentifier:identifier forLocalNotification:notification withResponseInfo:responseInfo completionHandler:completionHandler];
    }
}
 
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler{
    
    for(id<UIApplicationDelegate> obj in moduleArray){
        if([obj respondsToSelector:@selector(application:didReceiveRemoteNotification:fetchCompletionHandler:)])
        [obj application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
    }
    
}

- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler{
    
    for(id<UIApplicationDelegate> obj in moduleArray){
        if([obj respondsToSelector:@selector(application:performFetchWithCompletionHandler:)])
        [obj application:application performFetchWithCompletionHandler:completionHandler];
    }
}

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void(^)(BOOL succeeded))completionHandler{
    
    for(id<UIApplicationDelegate> obj in moduleArray){
        if([obj respondsToSelector:@selector(application:performActionForShortcutItem:completionHandler:)])
        [obj application:application performActionForShortcutItem:shortcutItem completionHandler:completionHandler];
    }
}

- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler{
    for(id<UIApplicationDelegate> obj in moduleArray){
        if([obj respondsToSelector:@selector(application:handleEventsForBackgroundURLSession:completionHandler:)])
        [obj application:application handleEventsForBackgroundURLSession:identifier completionHandler:completionHandler];
    }
}

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(nullable NSDictionary *)userInfo reply:(void(^)(NSDictionary * __nullable replyInfo))reply{
    for(id<UIApplicationDelegate> obj in moduleArray){
        if([obj respondsToSelector:@selector(application:handleWatchKitExtensionRequest:reply:)])
        [obj application:application handleWatchKitExtensionRequest:userInfo reply:reply];
    }
}

- (void)applicationShouldRequestHealthAuthorization:(UIApplication *)application{
    for(id<UIApplicationDelegate> obj in moduleArray){
        if([obj respondsToSelector:@selector(applicationShouldRequestHealthAuthorization:)])
        [obj applicationShouldRequestHealthAuthorization:application];
    }
}

- (void)applicationProtectedDataWillBecomeUnavailable:(UIApplication *)application{
    for(id<UIApplicationDelegate> obj in moduleArray){
        if([obj respondsToSelector:@selector(applicationProtectedDataWillBecomeUnavailable:)])
        [obj applicationProtectedDataWillBecomeUnavailable:application];
    }
}

- (void)applicationProtectedDataDidBecomeAvailable:(UIApplication *)application{
    for(id<UIApplicationDelegate> obj in moduleArray){
        if([obj respondsToSelector:@selector(applicationProtectedDataDidBecomeAvailable:)])
        [obj applicationProtectedDataDidBecomeAvailable:application];
    }
}

@end
